﻿Imports System.Data.SqlServerCe

Public Class Buddies
    Private Const mBuddyColumns As String = "id,name,host,port,lastconnected,comment,pairuppincode,pairupvaliduntil,passcodemine,passcodebuddy,uniqueId,email"
    Shared Sub New()
        Dim aCols() As String = {"Id", "Name", "Host", "Port" _
                               , "LastConnected", "Comment" _
                               , "PairUpPinCode", "PairUpValidUntil" _
                               , "PassCodeMine/PassCode_LoginHere", "PassCodeBuddy/PassCode_LoginAtBuddy" _
                               , "UniqueId", "Email"}
        ObjectPersister.DefineObject(GetType(Entities.Buddy), "Buddies", aCols)
    End Sub

    Public Shared Event BuddyUpdated(ByVal buddy As Entities.Buddy)
    Private Shared Sub OnBuddyUpdated(ByVal buddy As Entities.Buddy)
        Utils.Threading.Invoke(AddressOf OnBuddyUpdated_Callback, buddy)
    End Sub
    Private Shared Sub OnBuddyUpdated_Callback(ByVal buddy As Entities.Buddy)
        RaiseEvent BuddyUpdated(buddy)
    End Sub
    Public Shared Event BuddyCreated(ByVal buddy As Entities.Buddy)
    Private Shared Sub OnBuddyCreated(ByVal buddy As Entities.Buddy)
        Utils.Threading.Invoke(AddressOf OnBuddyCreated_Callback, buddy)
    End Sub
    Private Shared Sub OnBuddyCreated_Callback(ByVal buddy As Entities.Buddy)
        RaiseEvent BuddyCreated(buddy)
    End Sub

    Public Shared Event BuddyDeleted(ByVal buddy As Entities.Buddy)
    Private Shared Sub OnBuddyDeleted(ByVal buddy As Entities.Buddy)
        Utils.Threading.Invoke(AddressOf OnBuddyDeleted_Callback, buddy)
    End Sub
    Private Shared Sub OnBuddyDeleted_Callback(ByVal buddy As Entities.Buddy)
        RaiseEvent BuddyDeleted(buddy)
    End Sub

    Private Sub New()
    End Sub

    Public Shared Sub Delete(ByVal buddy As Entities.Buddy)
        Dim strSql As String = String.Format("DELETE FROM Buddies WHERE Id={0}", buddy.Id)
        Dim iRowsAffected As Integer = Environment.ExecuteNonQuery(strSql)
        OnBuddyDeleted(buddy)
    End Sub

    Public Shared Sub Save(ByVal buddy As Entities.Buddy)
        Dim clsCmd As New SaveCommand(Of Integer)("Buddies", "Id", buddy.Id, 0)
        clsCmd.AddParam("Name", buddy.Name, True)
        clsCmd.AddParam("Host", buddy.Host, True)
        clsCmd.AddParam("Port", buddy.Port, True)
        clsCmd.AddParam("LastConnected", buddy.LastConnected, True)
        clsCmd.AddParam("Comment", buddy.Comment, True)
        clsCmd.AddParam("PairUpPinCode", buddy.PairUpPinCode, True)
        clsCmd.AddParam("PairUpValidUntil", buddy.PairUpValidUntil, True)
        clsCmd.AddParam("PassCodeMine", buddy.PassCode_LoginHere, True)
        clsCmd.AddParam("PassCodeBuddy", buddy.PassCode_LoginAtBuddy, True)
        clsCmd.AddParam("Email", buddy.Email, True)
        clsCmd.AddParam("UniqueId", buddy.UniqueId, True)

        buddy.Id = clsCmd.Save

        If clsCmd.IsInsert Then
            OnBuddyCreated(buddy)
        Else
            OnBuddyUpdated(buddy)
        End If
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Buddy)
        Return ObjectPersister.GetAll(Of Entities.Buddy)()
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Buddy
        Return ObjectPersister.GetOne(Of Entities.Buddy)(String.Format("Id={0}", id))
    End Function
    Public Shared Function GetByUniqueId(ByVal uniqueId As String) As Entities.Buddy
        Return ObjectPersister.GetOne(Of Entities.Buddy)(String.Format("UniqueId='{0}'", uniqueId))
    End Function

End Class
